<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>7-继承.html</title>
    <style>
      #box {
        position: absolute;
        left: 0;
        top: 0;
        width: 100px;
        height: 100px;
        background: red;
        cursor: pointer;
      }
      #box2 {
        position: absolute;
        left: 100px;
        top: 0;
        width: 100px;
        height: 100px;
        background: green;
        cursor: pointer;
      }
    </style>
  </head>
  <body>
    <div id="box"></div>
    <div id="box2"></div>
    <script>
      class Parent {
        constructor(name) {
          this.name = name;
        }
        sayName() {
          console.log(this.name);
        }
      }

      // class Child  {
      //   constructor(name, age) {
      //     this.name = name;
      //     this.age = age;
      //   }
      //   sayName() {
      //     console.log(this.name);
      //   }
      //   sayAge() {
      //     console.log(this.age);
      //   }
      // }

      // 继承：把父类的属性和方法都拿过来
      class Child extends Parent {
        constructor(name, age) {
          super(name); // 继承父类的构造函数
          this.age = age;
        }
        sayAge() {
          console.log(this.age);
        }
      }

      let child = new Child("KKB", 18)
      child.sayName()
      child.sayAge()
    </script>
  </body>
</html>
